package com.wk.jetpackdemo.model

import android.databinding.ObservableBoolean
import android.util.Log
import com.wk.jetpackdemo.entities.Article
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers

class PaoViewModel : BaseViewModel() {
    val articleList: CustomObservableList<Article> = CustomObservableList()
    val loading: ObservableBoolean = ObservableBoolean(false)

    var page = 1
    override fun loadData(page: Int) {
        PaoRepository.getArticleList(page = this.page)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .doOnSubscribe {
                    Log.e(TAG, "doOnSubscribe:$it")
                    loading.set(true)
                }
                .doOnSuccess {
                    Log.e(TAG, "doOnSuccess:$it")
                    it ?: return@doOnSuccess
                    it.items ?: return@doOnSuccess
                    it.items.forEach { article ->
                        Log.e(TAG, "article:$article")
                    }
                    articleList.clear(false)
                    articleList.addAll(it.items, true)
                }.doOnError {
                    Log.e(TAG, "error:$it")
                }
                .doAfterTerminate {
                    Log.e(TAG, "doAfterTerminate")
                    loading.set(false)
                }.subscribe()
//        this.page++
    }

    private val TAG: String = "PaoViewModel"
}